///////////////////////////////////////
//added by nvcuong	///////////////////
///////////////////////////////////////
#include "_string.h"

int IsNumber(char *str)
{
	unsigned int i;
	for(i = 0; i < strlen(str); i++)
	{
		if( str[i]<'0' || str[i]>'9' )
			return 0;
	}
	return 1;
}
int IsDouble(char *str)
{
	unsigned int i;
	int check = 0;
	if(strcmp(str,"")==0)
		return 0;
	for(i= 0; i < strlen(str); i++)
	{
		if( !check && str[i]=='.' )
			check = 1;
		else if( str[i]<'0' || str[i]>'9' )
			return 0;
	}
	return 1;
}
int IsString(char *str)
{
	return (!IsNumber(str))&&(!IsDouble(str));
}

char* DelAt(char *str,unsigned int nIndex)
{
	unsigned long nLength = strlen(str);
	unsigned long i;
	char *kq;
	kq = malloc(nLength-1);
	if(nIndex < 0 || nIndex > nLength)
		return NULL;
	for(i = 0; i < nIndex; i++)
		kq[i] = str[i];
	for(i = nIndex; i < nLength; i++)
		kq[i] = str[i+1];
	return kq;

}
char* TrimLeft(char *str)
{
	char *kq = malloc(strlen(str));
	unsigned long i;
	int check = 0;
	strcpy(kq,str);
	for(i= 0; i < strlen(kq) && !check; i++)
		if(kq[i]==' '||kq[i]=='\n'||kq[i]=='\t')
		{
			kq = DelAt(kq,i);
			i--;
		}
		else
			check = 1;
	return kq;
}
char* TrimRight(char *str)
{
	unsigned long i; 
	char *kq = malloc(strlen(str));
	int check = 0;
	strcpy(kq,str);
	for(i= strlen(kq)-1; i >= 0 && !check; i--)
		if(kq[i]==' '||kq[i]=='\n'||kq[i]=='\t')
		{
			kq = DelAt(kq,i);
			//i++;
		}
		else
			check = 1;
	return kq;
}
char* Trim(char *str)
{
	char *kq = TrimLeft(str);
	kq = TrimRight(kq);
	return kq;
}


void SubString(char *sSub, char *sSource, long nStart, long nLength)	 
{
	long iSource = nStart;
	long iSub = 0;
	for(;iSub < nLength;iSub++)
	{
		sSub[iSub] = sSource[iSource++];
	}
	sSub[iSub] = 0; //ket thuc chuoi Sub
}
void InsertChar(char *s,char c,int pos)
{
	long k;
	long len;
	len = strlen(s);
	for( k = len+1; k > pos; k--)
	{
		s[k] = s[k-1];
	}
	s[pos] = c;		   
}
void DelChar(char *s, int pos)
{
	unsigned long k;
	for(k = pos; k<strlen(s); k++)
		s[k] = s[k+1];
	s[k] = 0; //ket thuc chuoi~
}
int Contains(char *s, char *s1)
{
	long len = strlen(s);
	long len1 = strlen(s1);
	long i;
	long j;
	for( i = 0; i <= len-len1; i++)
	{
		for( j = 0; j < len1 && s[i+j]==s1[j] ; j++);
		if( j == len1 )
		{
			return i;
		}			
	}
	return -1;
}

void DelString(char *s, long start, long end)
{
	long k;
	if(start < 0)
		start = 0;
	if(end < 0)
		end = 0;
	if(end >= strlen(s))
		end = strlen(s);
	for(k = start; k<=end; k++)
		DelChar(s,start);
}

/* Delete all comment*/
void DelComment(char *s)
{
	unsigned long i;
	long start;
	long end;
	while(1)
	{
		start = Contains(s,"/*");
		if(start !=-1)
		{
			end = Contains(s,"*/") + 1;
//added by pdvnq
				if(end < start) 
					break;
////////////////
			DelString(s,start,end);
		}
		else
		{
			start = Contains(s, "//");
			if(start!=-1)
			{
				char c[2];
				c[0] = 10;
				c[1] = 0;
				end = start + Contains(s+start,c) -1;
//added by pdvnq
				if(end < start) 
					break;
////////////////
				DelString(s,start,end);
			}
			else
				break;
		}
	}
}
